import React, { useEffect, useState, useRef } from 'react';
import { jsapi, layerCreator, viewUtils, layerUtils } from '@/utils/arcgis';


let highlightSelect = []

/**
 * 统一管理高亮，并且在组件解除挂载时清除高亮 
 * @param {*} cfg 
 * @returns 
 */
function useHighlight(cfg) {
  useEffect(() => {
    return clearHighlight
  }, []);

  async function setHighlight(layer, graphics) {
    clearHighlight()
    const view = await viewUtils.isViewReady();
    if (!Array.isArray(graphics)) {
      graphics=[graphics]
    }
    view.whenLayerView(layer).then(function(layerView) {
      graphics.forEach(e => {
        highlightSelect.push(layerView.highlight(e));
      });
    });
  }

  function clearHighlight() {
    try {
      if (highlightSelect.length) {
        highlightSelect.forEach(e => e.remove());
        highlightSelect = [];
      }
    } catch (error) {}
  }


  return [
    setHighlight,
    clearHighlight
  ];
}

export default useHighlight;
